﻿Imports System.Data.SqlServerCe

Namespace Data
    Public Class MetaData
        Public Shared Function GetDBVersion(ByVal source As Source) As Integer
            Return GetValueAsInteger("VERSION", 0, source)
        End Function
        Public Shared Function GetDBVersion(ByVal filePath As String, ByVal password As String) As String
            Return GetValueAsString("VERSION", "0.0", filePath, password)
        End Function
        Friend Shared Sub SetDBVersion(ByVal value As Integer, ByVal transactionScope As TransactionScope)
            SetValue("VERSION", value, transactionScope)
        End Sub

        Public Shared Function GetValueAsString(ByVal key As String, ByVal [default] As String) As String
            Return GetValueAsString(key, [default], Source.Local)
        End Function
        Public Shared Function GetValueAsInteger(ByVal key As String, ByVal [default] As Integer) As Integer
            Return GetValueAsInteger(key, [default], Source.Local)
        End Function
        Public Shared Function GetValueAsInteger(ByVal key As String, ByVal [default] As Integer, ByVal source As Source) As Integer
            Dim sVal As String = GetValueAsString(key, [default].ToString, source)
            Dim iRet As Integer = [default]
            Integer.TryParse(sVal, iRet)
            Return iRet
        End Function
        Public Shared Function GetValueAsBoolean(ByVal key As String, ByVal [default] As Boolean) As Boolean
            Dim sVal As String = GetValueAsString(key, CStr(IIf([default], "1", "0")), Source.Local)
            Return sVal = "1"
        End Function

        Private Shared Function GetValueAsString(ByVal key As String, ByVal [default] As String, ByVal source As Source) As String
            Dim strSql As String = "SELECT [Value] FROM MetaData WHERE [Key]=@Key"
            Dim sqlCmd As Command = Environment.NewCommand(strSql, source)
            sqlCmd.AddParameterWithValue("@Key", key)
            sqlCmd.Connection.Open()
            Try
                Environment.OnDBTraceInfoCommand(sqlCmd)
                Using dr As SqlCeDataReader = sqlCmd.ExecuteReader()
                    If Not dr.Read Then
                        Return [default]
                    Else
                        Return dr.GetString(0)
                    End If
                End Using
            Finally
                sqlCmd.Connection.Close()
            End Try
        End Function

        Private Shared Function GetValueAsString(ByVal key As String, ByVal [default] As String, ByVal filePath As String, ByVal password As String) As String
            Dim strSql As String = "SELECT [Value] FROM MetaData WHERE [Key]=@Key"
            Dim sqlCmd As Command = Environment.NewCommand(strSql, filePath, password)
            sqlCmd.AddParameterWithValue("@Key", key)
            sqlCmd.Connection.Open()
            Try
                Using dr As SqlCeDataReader = sqlCmd.ExecuteReader()
                    If Not dr.Read Then
                        Return [default]
                    Else
                        Return dr.GetString(0)
                    End If
                End Using
            Finally
                sqlCmd.Connection.Close()
            End Try
        End Function

        Public Shared Sub SetValue(ByVal key As String, ByVal value As Integer, ByVal transactionScope As TransactionScope)
            SetValue(key, value.ToString, transactionScope)
        End Sub
        Public Shared Sub SetValue(ByVal key As String, ByVal value As Boolean, ByVal transactionScope As TransactionScope)
            SetValue(key, CStr(IIf(value, "1", "0")), transactionScope)
        End Sub
        Public Shared Sub SetValue(ByVal key As String, ByVal value As String)
            Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
                SetValue(key, value, t)
                t.Complete()
            End Using
        End Sub
        Public Shared Sub SetValue(ByVal key As String, ByVal value As String, ByVal transactionScope As TransactionScope)
            Dim strSql As String = "UPDATE MetaData SET [Value]=@Value WHERE [Key]=@Key"
            Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
            sqlCmd.AddParameterWithValue("@Key", key)
            sqlCmd.AddParameterWithValue("@Value", value)
            Dim iAffectedRows As Integer = sqlCmd.ExecuteNonQuery()
            If iAffectedRows = 0 Then
                sqlCmd.CommandText = "INSERT INTO MetaData ([Key],[Value]) VALUES (@Key,@Value)"
                iAffectedRows = sqlCmd.ExecuteNonQuery
            End If
        End Sub

    End Class

End Namespace